package com.wva.captcha.controller;

import com.wva.captcha.domain.dto.GetSmsCaptchaDTO;
import com.wva.captcha.domain.vo.GetPictureCaptchaVO;
import com.wva.captcha.service.CaptchaService;
import com.wva.common.annotation.authority.Open;
import com.wva.common.domain.dto.PictureCaptchaVerifyDTO;
import com.wva.common.domain.dto.SmsCaptchaVerifyDTO;
import com.wva.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/captcha")
@Api(tags = "验证码微服务接口")
@Slf4j
@Open
public class CaptchaController {
    @Autowired
    CaptchaService captchaService;

    @ApiOperation("获得图形验证码")
    @GetMapping("/picture")
    R<GetPictureCaptchaVO> getPictureCaptcha() {
        GetPictureCaptchaVO vo = captchaService.getPictureCaptcha();
        return R.ok(vo);
    }

    @ApiOperation("获得短信验证码")
    @PostMapping("/sms")
    R<Object> getSmsCaptcha(@Validated @RequestBody GetSmsCaptchaDTO getSmsCaptchaDTO) {
        captchaService.getSmsCaptcha(getSmsCaptchaDTO);
        return R.ok().message("短信验证码发送成功，请注意查收");
    }

    @ApiOperation("校验图形验证码")
    @PostMapping("/verify/picture")
    R<Object> verifyPictureCaptcha(@Validated @RequestBody PictureCaptchaVerifyDTO pictureCaptchaVerifyDTO) {
        captchaService.verifyPictureCaptcha(pictureCaptchaVerifyDTO);
        return R.ok();
    }

    @ApiOperation("校验短信验证码")
    @PostMapping("/verify/sms")
    R<Object> verifySmsCaptcha(@Validated @RequestBody SmsCaptchaVerifyDTO smsCaptchaVerifyDTO) {
        captchaService.verifySmsCaptcha(smsCaptchaVerifyDTO);
        return R.ok();
    }
}
